package br.com.ordersys.web.client.modules.main;

import br.com.ordersys.web.client.modules.events.BroadcastDispatcher;
import br.com.ordersys.web.client.modules.events.BroadcastEvent;
import br.com.ordersys.web.client.modules.events.BroadcastEventHandler;
import br.com.ordersys.web.client.modules.events.BroadcastMessage;
import br.com.ordersys.web.client.modules.uicomp.BallonPopup;
import br.com.ordersys.web.client.modules.uicomp.ClickableHPanel;
import br.com.ordersys.web.client.modules.uicomp.FallbackImage;
import br.com.ordersys.web.client.modules.uicomp.OptionPopup;
import br.com.ordersys.web.client.modules.utils.I18nBundle;
import br.com.ordersys.web.client.modules.utils.UIUtils;

import com.google.gwt.core.client.EntryPoint;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.event.logical.shared.CloseEvent;
import com.google.gwt.event.logical.shared.CloseHandler;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.ui.DeckPanel;
import com.google.gwt.user.client.ui.DockPanel;
import com.google.gwt.user.client.ui.Frame;
import com.google.gwt.user.client.ui.HTML;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.Panel;
import com.google.gwt.user.client.ui.PopupPanel;
import com.google.gwt.user.client.ui.PushButton;
import com.google.gwt.user.client.ui.RootPanel;

/**
 * Entry point classes define <code>onModuleLoad()</code>.
 */
public class MainWindowUI implements EntryPoint {
	DeckPanel centerPanel = null;
	I18nBundle i18n = new I18nBundle("mainWindow");
	
	static{
		MainWindowResources.INSTANCE.mainWindowStyle().ensureInjected();
	}

	/**
	 * This is the entry point method.
	 */
	public void onModuleLoad() {
		DockPanel mainPnl = new DockPanel();

		mainPnl.setWidth("100%");
		mainPnl.setHeight("100%");

		RootPanel.get("_main").add(mainPnl);

		mainPnl.add(buildTaskBar(), DockPanel.NORTH);
		mainPnl.add(buildCenterPane(), DockPanel.CENTER);
	}

	private Panel buildTaskBar() {
		HorizontalPanel taskBarPnl = new HorizontalPanel();
		taskBarPnl.getElement().setId("mainWindow_taskBar");
		
		taskBarPnl.setStyleName(MainWindowResources.INSTANCE.mainWindowStyle().taskBarPanel());

		PushButton menuBtn = new PushButton();
		menuBtn.setStyleName(MainWindowResources.INSTANCE.mainWindowStyle().menuButton());

		menuBtn.addClickHandler(new ClickHandler() {

			@Override
			public void onClick(ClickEvent e) {
				int index = centerPanel.getVisibleWidget() + 1;

				if (index >= centerPanel.getWidgetCount()) {
					index = 0;
				}

				centerPanel.showWidget(index);
			}
		});
		taskBarPnl.add(menuBtn);
		taskBarPnl.setCellVerticalAlignment(menuBtn, HorizontalPanel.ALIGN_MIDDLE);

		HorizontalPanel rightPanel = new HorizontalPanel();
		taskBarPnl.add(rightPanel);
		taskBarPnl.setCellHorizontalAlignment(rightPanel, taskBarPnl.ALIGN_RIGHT);
		taskBarPnl.setCellVerticalAlignment(rightPanel, taskBarPnl.ALIGN_MIDDLE);

		buildUserOptionsPanel(rightPanel);

		buildExitButton(rightPanel);

		return taskBarPnl;
	}

	private void buildExitButton(HorizontalPanel taskBarPnl) {
		HTML exitBtn = new HTML();
		exitBtn.setStyleName(MainWindowResources.INSTANCE.mainWindowStyle().taskBarButton());

		UIUtils.setWidgetStyle(exitBtn, "backgroundImage: url('images/exit.png')");
		taskBarPnl.add(exitBtn);

		taskBarPnl.setCellHorizontalAlignment(exitBtn, taskBarPnl.ALIGN_RIGHT);
		taskBarPnl.setCellVerticalAlignment(exitBtn, taskBarPnl.ALIGN_MIDDLE);

		exitBtn.addClickHandler(new ClickHandler() {
			@Override
			public void onClick(ClickEvent e) {
				OptionPopup.showConfirmationDialog(i18n.get("msg_confirm_exit"), "300px", "120px", new Runnable() {
					@Override
					public void run() {
						Window.Location.assign(UIUtils.buildAppBaseURI("Logout"));
					}
				});
			}
		});
	}

	public void buildUserOptionsPanel(HorizontalPanel taskBarPnl) {
		//final ClickableHPanel hPnl = new ClickableHPanel("userPanel");
		final ClickableHPanel hPnl = new ClickableHPanel(MainWindowResources.INSTANCE.mainWindowStyle().userPanel(),MainWindowResources.INSTANCE.mainWindowStyle().userPanelPressed());

		// HTML userPicture = new HTML();
		final FallbackImage userPicture = new FallbackImage("img/user/" + UIUtils.getEnvironment("userId"));
		userPicture.setFallbackURL("images/silueta-homen-branca.png");
		
		// images/silueta-homen-branca.png
		userPicture.setStyleName(MainWindowResources.INSTANCE.mainWindowStyle().userButton());
		userPicture.setSize("2.5em", "2.5em");

		hPnl.add(userPicture);
		hPnl.setCellVerticalAlignment(userPicture, HorizontalPanel.ALIGN_MIDDLE);
		hPnl.setCellHorizontalAlignment(userPicture, HorizontalPanel.ALIGN_RIGHT);

		HTML arrow = new HTML();
		arrow.setStyleName(MainWindowResources.INSTANCE.mainWindowStyle().arrowDownButton());

		hPnl.add(arrow);
		hPnl.setCellVerticalAlignment(arrow, HorizontalPanel.ALIGN_MIDDLE);
		hPnl.setCellHorizontalAlignment(arrow, HorizontalPanel.ALIGN_RIGHT);

		taskBarPnl.add(hPnl);
		taskBarPnl.setCellHorizontalAlignment(hPnl, HorizontalPanel.ALIGN_RIGHT);
		taskBarPnl.setCellVerticalAlignment(hPnl, HorizontalPanel.ALIGN_MIDDLE);

		hPnl.setPressHandler(new ClickableHPanel.PressHandler() {
			BallonPopup popup;

			@Override
			public void onUnPress(ClickEvent e) {
				if (popup != null) {
					popup.hide();
					popup = null;
				}
			}

			@Override
			public void onPress(ClickEvent e) {
				popup = new BallonPopup();

				popup.setSize("30em", "13em");

				popup.addContentWidget(new UserOptionsPanel(popup));

				popup.addCloseHandler(new CloseHandler<PopupPanel>() {
					@Override
					public void onClose(CloseEvent<PopupPanel> e) {
						hPnl.unpress();
					}
				});

				popup.addAutoHidePartner(hPnl.getElement()); // isso evita que ao clicar no botão o popup se feche e abra novamente em siguida por conta da sequencia de eventos

				popup.showPopUpRelativeTo(hPnl);
			}
		});
		
		BroadcastDispatcher.addHandler(new BroadcastEventHandler() {
			 
			@Override
			public void onEvent(BroadcastEvent e) {
				if(e.getMessage() == BroadcastMessage.USER_PICTURE_CHANGED){
					userPicture.reload();
				}
			}
		});

	}

	public Panel buildCenterPane() {
		centerPanel = new DeckPanel();

		centerPanel.setStyleName(MainWindowResources.INSTANCE.mainWindowStyle().centerPanel());
		centerPanel.getElement().setId("mainWindow_centerPanel");
		
		MainMenu mn = new MainMenu();
		mn.getElement().setId("mainWindow_mainMenu");
		mn.setStyleName(MainWindowResources.INSTANCE.mainWindowStyle().contentFrame());
		
		Label l1 = new Label("label01");
		Label l2 = new Label("label02");

		centerPanel.add(l1);
		centerPanel.add(l2);
		centerPanel.add(mn);

		mn.load();
		
		Frame frm = new Frame("http://www.uol.com.br");
		frm.setStyleName(MainWindowResources.INSTANCE.mainWindowStyle().contentFrame());

		centerPanel.add(frm);

		return centerPanel;
	}

}
